<?php
/**
 * 模板03 初步人选名单
 */
namespace App\Admin\Renderables\Ddb;

use App\Models\DdbCbrx;
use Dcat\Admin\Support\LazyRenderable;
use Dcat\Admin\Widgets\Table;
use Illuminate\Support\Facades\DB;

class StatisticForDdbCbrxRenderTable extends LazyRenderable
{
    public function render()
    {
        $counts = DdbCbrx::query()->select(DB::raw('dwmc, dwdm, count(*) as count'))->groupBy('dwdm')->get()->toArray();
        $counts = collect($counts)->keyBy('dwdm')->all();

        $headers = ['选举单位', '推荐代表数'];
        $rows = [];
        foreach(dws() as $dwdm => $dwmc) {
            $count = isset($counts[$dwdm]) ? $counts[$dwdm]['count'] : 0;
            array_push($rows, [
                $dwmc,
                $count
            ]);
        }
        $table = Table::make($headers, $rows)->withBorder()->render();
        return $table;
    }
}